<?php


namespace App\Http\Model\Job;


use App\Enum\ClassAmtTransferEnum;
use App\Enum\ClassListPayResultHistoryEnum;
use Illuminate\Support\Facades\DB;

class ClassAmtRefreshModel
{


    /**
     * 更新班级余额表
     *
     * @param $data
     * @param $class
     * @param $payResultHistory
     * @param $payResultHistorySum
     * @param $amtTransferDetail
     *
     * @return bool|int
     */
    public static function save($data, $class, $payResultHistory, $payResultHistorySum, $amtTransferDetail)
    {
        $totalPaymentNetAmount = isset($payResultHistory->total_pay_amt) ? $payResultHistory->total_pay_amt : '0';
        $arr = [
            /*招行&数仓-业务线分校配置信息主键ID*/
            'pfs_dw_id'                   => $data['pfs_dw_id'],
            /*汇总时间*/
            'summarize_time'              => $data['date'],
            /*城市名称*/
            'city_name'                   => $class->city_name,
            /*主体ID*/
            'ledger_subject_id'           => $class->ledger_subject_id,
            /*主体名称*/
            'ledger_subject_name'         => $class->ledger_subject_name,
            /*班级编号，统一机构下确保唯一*/
            'class_id'                    => $class->class_id,
            /*班级id*/
            'org_class_id'                => $class->org_class_id,
            /*班级名称*/
            'class_name'                  => $class->class_name,
            /*累计收款净额*/
            'total_payment_net_amount'    => $totalPaymentNetAmount,
            /*实际收款净额(累计)*/
            'reality_payment_net_amount'  => $payResultHistorySum['reality_payment_net_amount'],
            /*累计划拨金额*/
            'total_transfer_amount'       => $amtTransferDetail['total_transfer_amount'],
            /*累计划拨次数*/
            'total_transfer_count'        => $amtTransferDetail['total_transfer_count'],
            /*预计未划拨余额*/
            'plan_not_transfer_amount'    => bcsub($totalPaymentNetAmount, $amtTransferDetail['total_transfer_amount']),
            /*实际未划拨余额*/
            'reality_not_transfer_amount' => bcsub($payResultHistorySum['reality_payment_net_amount'], $amtTransferDetail['total_transfer_amount']),
            /*更新时间*/
            'utime'                       => date('Y-m-d H:i:s'),
        ];
        /*查询当前汇总日期 班级余额 记录*/
        $exist = DB::select('SELECT `id` FROM `class_balance` WHERE `pfs_dw_id`=:pfs_dw_id AND `class_id`=:class_id AND `summarize_time`=:summarize_time AND `is_del`=0', [
            'pfs_dw_id'      => $data['pfs_dw_id'],
            'class_id'       => $data['class_id'],
            'summarize_time' => $data['date'],
        ]);
        if (empty($exist)) {
            /*新增*/
            return DB::table('class_balance')->insert($arr);
        }
        /*更新*/
        return DB::table('class_balance')->where('id', current($exist)->id)->update($arr);
    }


    /**
     * 查询截止当期日期需要计算的 累计划拨 数
     *
     * @param $data
     *
     * @return string[]
     */
    public static function amtTransferDetail($data)
    {
        $list = DB::select('SELECT `pfs_transfer_status`,`pfs_transfer_amt` FROM `class_amt_transfer_detail` WHERE `pfs_dw_id`=:pfs_dw_id AND `class_id`=:class_id AND `create_date`<=:create_date AND `is_del`=0', [
            'pfs_dw_id'   => $data['pfs_dw_id'],
            'class_id'    => $data['class_id'],
            'create_date' => $data['date'],
        ]);
        $amtTransferDetail = ['total_transfer_amount' => '0', 'total_transfer_count' => '0'];
        if (empty($list)) {
            return $amtTransferDetail;
        }
        foreach ($list as $val) {
            if ($val->pfs_transfer_status === ClassAmtTransferEnum::PFS_STATUS_S) {
                /*累加 本次划拨金额（银行端计算结果）*/
                $amtTransferDetail['total_transfer_amount'] = bcadd($val->pfs_transfer_amt, $amtTransferDetail['total_transfer_amount']);
                /*累计划拨次数*/
                $amtTransferDetail['total_transfer_count'] = bcadd($amtTransferDetail['total_transfer_count'], 1);
            }
        }
        return $amtTransferDetail;
    }


    /**
     * 查询截止当前日期需要累计计算的数
     *
     * @param $data
     *
     * @return string[]
     */
    public static function payResultHistorySum($data)
    {
        $list = DB::select('SELECT `result`,`pay_amt`,`refund_amt` FROM `class_list_pay_result_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `class_id`=:class_id AND `create_date`<=:create_date', [
            'pfs_dw_id'   => $data['pfs_dw_id'],
            'class_id'    => $data['class_id'],
            'create_date' => $data['date'],
        ]);
        $result = ['reality_payment_net_amount' => '0', 'pay_amt_sum' => '0', 'refund_amt_sum' => '0'];
        if (empty($list)) {
            return $result;
        }
        foreach ($list as $val) {
            /*累加 已经推送成功的金额*/
            if ($val->result === ClassListPayResultHistoryEnum::RESULT_SUCCESS) {
                $result['pay_amt_sum'] = bcadd($result['pay_amt_sum'], $val->pay_amt);
                $result['refund_amt_sum'] = bcadd($result['refund_amt_sum'], $val->refund_amt);
            }
        }
        /*实际累计收款净额*/
        $result['reality_payment_net_amount'] = bcsub($result['pay_amt_sum'], $result['refund_amt_sum']);
        return $result;
    }


    /**
     * 查询截止当前汇总日期的汇总记录
     *
     * @param $data
     *
     * @return array|false|mixed
     */
    public static function payResultHistory($data)
    {
        $result = DB::select('SELECT `total_pay_amt`,`total_refund_amt` FROM `class_list_pay_result_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `class_id`=:class_id AND `create_date`<=:create_date ORDER BY `create_date` DESC LIMIT 1', [
            'pfs_dw_id'   => $data['pfs_dw_id'],
            'class_id'    => $data['class_id'],
            'create_date' => $data['date'],
        ]);
        return empty($result) ? [] : current($result);
    }


    /**
     * 查询指定班级信息
     *
     * @param $data
     *
     * @return array|false|mixed
     */
    public static function class($data)
    {
        /*查询班级记录中的班级信息*/
        $history = DB::select('SELECT `org_class_id`,`class_id`,`class_name`,`ledger_subject_id`,`ledger_subject_name`,`city_name` FROM `class_list_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `class_id`=:class_id AND `create_date`=:create_date AND `is_del`=0 LIMIT 1', [
            'pfs_dw_id'   => $data['pfs_dw_id'],
            'class_id'    => $data['class_id'],
            'create_date' => $data['date'],
        ]);
        if (!empty($history)) {
            return current($history);
        }
        $class = DB::select('SELECT `org_class_id`,`class_id`,`class_name`,`ledger_subject_id`,`ledger_subject_name`,`city_name` FROM `class_list` WHERE `pfs_dw_id`=:pfs_dw_id AND `class_id`=:class_id AND `is_del`=0 LIMIT 1', [
            'pfs_dw_id' => $data['pfs_dw_id'],
            'class_id'  => $data['class_id'],
        ]);
        return empty($class) ? [] : current($class);
    }

}





/**
 * ClassAmtRefreshModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/9 21:51:00   操作:创建
 **/
